package com.gitee.wsl.ext.reflect

import kotlin.properties.Delegates
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


fun <T : Any> Delegates.writeOnce(initialValue: T): ReadWriteProperty<Any?, T> = object : ReadWriteProperty<Any?, T> {
    private var value: T = initialValue
    private var wasSet = false

     override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return value
    }

     override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        if (wasSet) throw IllegalStateException("Property ${property.name} cannot be set more than once.")
        this.value = value
        wasSet = true
    }
}
